﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;

namespace Loja.customer
{
    public partial class changePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string script = @"$(document).ready(function () {
                                $('.cancelar').button({icons: {primary: 'ui-icon-cancel'}});
                                $('.salvar').button({icons: {primary: 'ui-icon-disk'}});
                            });";
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "tela", script, true);

            if (Session["Cliente"] != null)
            {
                Cliente cliente = ((Cliente)Session["Cliente"]);
                Donini.Crypt.Crypt cript = new Donini.Crypt.Crypt();
                cript.Key = "chocolate";
                string _Senha = cript.Decrypt(cliente.CliSenha);
                hdPassword.Value = _Senha;
            }

        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                using (LojaChocolateDCDataContext dc = new LojaChocolateDCDataContext())
                {
                    Table<Cliente> tblCliente = dc.GetTable<Cliente>();
                    Cliente cliente = tblCliente.Where(c => c == ((Cliente)Session["Cliente"])).SingleOrDefault();
                    Donini.Crypt.Crypt cript = new Donini.Crypt.Crypt();
                    cript.Key = "chocolate";
                    cliente.CliSenha = cript.Encrypt(txtNewPassword.Text.Trim());
                    tblCliente.Context.SubmitChanges();

                    Response.Redirect(Page.ResolveUrl("../customer/") + "ListaPedidos.aspx");
                }
            }
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {

        }

        protected void validarSenha(object sender, ServerValidateEventArgs e)
        {            
            e.IsValid = hdPassword.Value.Trim() == e.Value.Trim();
        }
    }
}